//
//  CatogryGiftDetailViewController.m
//  project
//
//  Created by lanou on 15/10/30.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "CatogryGiftDetailViewController.h"
#import "HotListCell.h"
#import "PKRequestManager.h"
#import "HotDetailViewController.h"
#import "MJRefresh.h"
#import "ListModel.h"

@interface CatogryGiftDetailViewController () <UICollectionViewDataSource,UICollectionViewDelegate>

//创建一个collectionView
@property(nonatomic,strong)UICollectionView *giftDetailVC;
//创建一个数组来接收数据
@property(nonatomic,strong)NSMutableArray *dataSourceArray;

@end

@implementation CatogryGiftDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //给navigationItem添加title
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake((kWidth - 40) / 2, 10, 40, 20)];
    titleLabel.text = _titleString;
    titleLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = titleLabel;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    CGFloat item = (kWidth - 30)/2;
    layout.itemSize = CGSizeMake(item, item + 80);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    
    _giftDetailVC = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 10, kWidth, kHeight - 70) collectionViewLayout:layout];
    _giftDetailVC.backgroundColor = [UIColor whiteColor];
    _giftDetailVC.dataSource = self;
    _giftDetailVC.delegate = self;
    [_giftDetailVC registerClass:[HotListCell class] forCellWithReuseIdentifier:@"HotListCell"];
    _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    
    [self.view addSubview:_giftDetailVC];
    [self requestData];
    
    //上拉加载
    __weak CatogryGiftDetailViewController *newSelf = self;
    self.giftDetailVC.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [newSelf requestPullData];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotListCell" forIndexPath:indexPath];
    [cell ConfigModel: _dataSourceArray[indexPath.row]];
    return cell;
}

//推出下一个webView
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotDetailViewController *babyDetailVC = [[HotDetailViewController alloc]init];
    babyDetailVC.model = _dataSourceArray[indexPath.row];
    [self.navigationController pushViewController:babyDetailVC animated:YES];
}

-(void)requestData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    
    [PKRequestManager requestWithTypeP:GET urlString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/item_subcategories/%@/items?limit=20&offset=0",_id] parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dict = dic[@"data"];
        NSArray *arr = dict[@"items"];
        for (NSDictionary *dicModel in arr)
        {
            ListModel *model = [[ListModel alloc]init];
            [model setValuesForKeysWithDictionary:dicModel];
            [_dataSourceArray addObject:model];
        }
        [_giftDetailVC reloadData];
        
    } error:^(NSError *error) {
        NSLog(@"数据请求失败");
    }];
}

//上拉加载
static int offset = 0;
-(void)requestPullData
{
    offset += 20;
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    
    [PKRequestManager requestWithTypeP:GET urlString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/item_subcategories/%@/items?limit=20&offset=0",_id] parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dict = dic[@"data"];
        NSArray *arr = dict[@"items"];
        for (NSDictionary *dicModel in arr)
        {
            ListModel *model = [[ListModel alloc]init];
            [model setValuesForKeysWithDictionary:dicModel];
            [_dataSourceArray addObject:model];
        }
        [_giftDetailVC reloadData];
        [_giftDetailVC.footer endRefreshing];
        
        
    } error:^(NSError *error) {
        NSLog(@"数据请求失败");
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
